


package ciicos.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ciicos.annotation.JnpfField;
import ciicos.base.ActionResult;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.base.UserInfo;
import ciicos.base.vo.DownloadVO;
import ciicos.config.ConfigValueUtil;
import ciicos.exception.DataException;
import ciicos.onlinedev.model.OnlineImport.ExcelImFieldModel;
import ciicos.onlinedev.model.OnlineImport.ExcelImportModel;
import ciicos.onlinedev.model.OnlineImport.VisualImportModel;
import ciicos.permission.entity.UserEntity;
import org.springframework.transaction.annotation.Transactional;
import ciicos.base.entity.ProvinceEntity;
import ciicos.model.linedevice.*;
import ciicos.model.linedevice.LineDevicePagination;
import ciicos.entity.*;
import ciicos.util.*;
import ciicos.base.util.*;
import ciicos.base.vo.ListVO;
import ciicos.util.context.SpringContext;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.Cleanup;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ciicos.entity.LineDeviceEntity;
import ciicos.service.LineDeviceService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import ciicos.util.GeneraterSwapUtil;

import java.util.stream.Collectors;
import java.util.*;

import cn.xuyanwu.spring.file.storage.FileInfo;


/**
 * 终端设备管理
 *
 * @版本： V3.2.0
 * @版权：
 * @作者： 企业研发中心
 * @日期： 2025-10-06
 */
@Slf4j
@RestController
@Api(tags = "终端设备管理", value = "line")
@RequestMapping("/api/line/LineDevice")
public class LineDeviceController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private LineDeviceService lineDeviceService;


    /**
     * 列表
     *
     * @param lineDevicePagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody LineDevicePagination lineDevicePagination) throws IOException {
        //树形判断是否有参数，有参数不造树
        List<LineDeviceEntity> list = lineDeviceService.getList(lineDevicePagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<LineDeviceListVO> listVO = JsonUtil.getJsonToList(list, LineDeviceListVO.class);
        for (LineDeviceListVO lineDeviceVO : listVO) {
            lineDeviceVO.setCode_name(lineDeviceVO.getCode());
            lineDeviceVO.setName_name(lineDeviceVO.getName());
            lineDeviceVO.setNumber_name(lineDeviceVO.getNumber());
            lineDeviceVO.setOrganizeid_name(generaterSwapUtil.comSelectValues(lineDeviceVO.getOrganizeid(), false));
            lineDeviceVO.setMasteruserid_name(generaterSwapUtil.userSelectValues(lineDeviceVO.getMasteruserid()));
            lineDeviceVO.setStatus_name(lineDeviceVO.getStatus());
            lineDeviceVO.setImages_name(generaterSwapUtil.getFileNameInJson(lineDeviceVO.getImages()));
            lineDeviceVO.setRemark_name(lineDeviceVO.getRemark());
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(lineDevicePagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param lineDeviceForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid LineDeviceForm lineDeviceForm) throws DataException {
        String b = lineDeviceService.checkForm(lineDeviceForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        }
        String mainId = RandomUtil.uuId();

        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        LineDeviceEntity entity = JsonUtil.getJsonToBean(lineDeviceForm, LineDeviceEntity.class);
        entity.setId(mainId);
        lineDeviceService.save(entity);


        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<LineDeviceInfoVO> info(@PathVariable("id") String id) {
        LineDeviceEntity entity = lineDeviceService.getById(id);
        LineDeviceInfoVO vo = JsonUtil.getJsonToBean(entity, LineDeviceInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<LineDeviceInfoVO> detailInfo(@PathVariable("id") String id) {
        ActionResult info = info(id);
        Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
        LineDeviceEntity entity = lineDeviceService.getById(id);
        LineDeviceInfoVO vo = JsonUtil.getJsonToBean(entity, LineDeviceInfoVO.class);

        //子表数据转换

        //附表数据转换

//添加到详情表单对象中
        vo.setCode(vo.getCode());

        vo.setName(vo.getName());

        vo.setNumber(vo.getNumber());

        vo.setOrganizeid(generaterSwapUtil.comSelectValues(vo.getOrganizeid(), false));

        vo.setMasteruserid(generaterSwapUtil.userSelectValues(vo.getMasteruserid()));

        vo.setStatus(vo.getStatus());

        vo.setImages(generaterSwapUtil.getFileNameInJson(vo.getImages()));

        vo.setRemark(vo.getRemark());


        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @param lineDeviceForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid LineDeviceForm lineDeviceForm) throws DataException {
        String b = lineDeviceService.checkForm(lineDeviceForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        }
        LineDeviceEntity entity = lineDeviceService.getById(id);
        if (entity != null) {
            LineDeviceEntity subentity = JsonUtil.getJsonToBean(lineDeviceForm, LineDeviceEntity.class);
            boolean b1 = lineDeviceService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        lineDeviceService.removeById(id);
        return ActionResult.success("删除成功");
    }


}
